

public class SparseMatrix {

        public TripleNode data[];
        public int rows;
        public int cols;
        public int nums;
    public SparseMatrix( int maxSize){
            data = new TripleNode[maxSize];
            for (int i = 0; i < data.length; i++) {
                data[i] = new TripleNode();
            }
            rows = 0;
            cols = 0;
            nums = 0;
        }
        public void printMaterix () {
            int i;
            System.out.println("稀疏矩阵的三元组存储结构为：");
            System.out.println("行数：" + rows + ",列数：" + cols + ",非零元素个数：" + nums);

            for (i = 0; i < nums; i++) {
                System.out.println(data[i].row + "\t" + data[i].column + "\t" + data[i].value);
            }

        }//三元组表的打印
            public void output0() {
              int[][] n = new int[rows][cols];
               for (int i= 0; i< nums; i++) {
               n[data[i].row][data[i].column] = data[i].value;
                }
                System.out.println("稀疏矩阵为：");
                for (int a = 0; a < rows; a++) {
                    for (int b = 0; b < cols; b++) {
                    System.out.print(n[a][b] + "  ");
                    }

                    System.out.println("");
                }
            }//用三元组表输出矩阵

   public SparseMatrix( int mat[][]){
            int i, j, k = 0, count = 0;
            rows = mat.length;
            cols = mat[0].length;
            for (i = 0; i < mat.length; i++) {
                for (j = 0; j < mat[i].length; j++) {
                    if (mat[i][j] != 0) {
                        count++;
                    }
                }
            }
            nums = count;
            data = new TripleNode[nums];
            for (i = 0; i < mat.length; i++) {
                for (j = 0; j < mat[i].length; j++) {
                    if (mat[i][j] != 0) {
                        data[k] = new TripleNode(i, j, mat[i][j]);
                        k++;
                    }
                }
            }//三元组表的建立

        }
        public SparseMatrix fasttranspose(){
             SparseMatrix tm=new SparseMatrix(nums);
             tm.cols=rows;
             tm.rows=cols;
             tm.nums=nums;
             int i,j=0,k=0;
             int[] num,copt;
             if(nums>0){
                 num=new int[cols];
                 copt=new int[rows];
                 for (i=0;i<cols;i++){
                     num[i]=0;
                 }
                 for (i=0;i<nums;i++){
                     j=data[i].column;
                     num[j]++;
                 }
                 copt[0]=0;
                 for (i=1;i<cols;i++){
                     copt[i]=copt[i-1]+num[i-1];
                 }
                 for (i=0;i<nums;i++){
                     j=data[i].column;
                     k=copt[j];
                     tm.data[k].row=data[i].column;
                     tm.data[k].column=data[i].row;
                     tm.data[k].value=data[i].value;
                     copt[j]++;
                 }
             }
            return tm;
        }//矩阵的快速转置
    public void output1() {
        int[][] n = new int[rows][cols];
        for (int i= 0; i< nums; i++) {
            n[data[i].row][data[i].column] = data[i].value;
        }
        System.out.println("转置后稀疏矩阵为：");
        for (int a = 0; a < rows; a++) {
            for (int b = 0; b < cols; b++) {
                System.out.print(n[a][b] + "  ");
            }

            System.out.println("");
        }//矩阵转置的打印
    }
    public void Add(int[][] a, int[][] b) {

        int c[][]=new int[a.length][a[0].length];

        if (a.length != b.length || a[0].length != b[0].length)
            System.out.println("矩阵不能相加");
        else {
            System.out.println("两矩阵相加为：");
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j <a[0].length; j++) {
                    int item = a[i][j] + b[i][j];

                        c[i][j] = item;
                        System.out.print(c[i][j] + "  ");

                }

                System.out.println();

            }

        }


    }
    public void multiple(int[][] a, int[][] b){
        int c[][]=new int[a.length][b[0].length];
        if(a[0].length!=b.length){
            System.out.print("矩阵不能相乘");
        }


        else{
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < b[0].length; j++) {
                    for (int k = 0; k < a[0].length; k++) {
                        c[i][j] += a[i][k] * b[k][j];
                    }
                }
            }
            System.out.println("相乘后矩阵为：");
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j <b[0].length; j++) {
                    System.out.print(c[i][j] + "  ");
                }
                System.out.println();
                }
        }

    }


}






